/*
 * @Author: Marte
 * @Date:   2016-10-24 10:59:19
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-10-31 09:16:50
 */

'use strict';

var _ = require('underscore');

module.exports = function(sequelize, DataTypes) {
    var Pic = sequelize.define('Pic', {
        fileFullName: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '文件全名',
            unique: true,
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                }
            }
        },
        fileName: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '文件名',
            unique: true,
            validate: {
                notEmpty: {
                    msg: '不允许为空'
                }
            }
        },
        origName: {
            type: DataTypes.STRING,
            comment: '原始文件名'
        }
    }, {
        charset: 'utf8',
        classMethods: {
            associate: function(models) {
                Pic.hasMany(models.PicTypeRemarkGroup, {
                    constraints: false
                });
            },
            fetch: function(op) {
                return this
                    .findAll(_.extend({
                        order: [
                            'createdAt'
                        ],
                        raw: true
                    }, op || {}));
            },
            fetchPageJsonAndCountAll: function(offset, size, s) {
                var self = this;

                return self.findAndCountAll({
                        order: [
                            'createdAt'
                        ],
                        offset: offset,
                        limit: size,
                        // raw: true
                    })
                    .then(function(result) {
                        return result;
                    });

            },
            fetchPageJson: function(offset, size, s) {
                var self = this;
                return self.findAll({
                        order: [
                            'createdAt'
                        ],
                        offset: offset,
                        limit: size,
                        // raw: true
                    })
                    .then(function(result) {
                        return result;
                    });

            }

        }
    });
    return Pic;
};
